# This file runs a Python program, making sure that stdout and stderr are
# flushed after every write.
# It also sends secret signals to indicate when the console should read
# a line.
import sys, os

# Makes sure that the stream is flushed after every write.
class FakeOutput:
	def __init__(self, stream):
		self.stream = stream

	def write(self, text):
		#sys.stderr.write('Printing ' + text)
		text_len = len(text)
		# Handle Windows console newlines
		if len(os.linesep) == 2:
			text_len += text.count('\n')
		self.stream.write('%06d' % text_len)
		self.stream.write(text)
		self.stream.flush()

# When input is requested, this writes #IN to stdout.
# That signal is intercepted to put the console into writing mode.
class FakeInput:
	def __init__(self, stream):
		self.stream = stream

	def readline(self):
		sys.__stdout__.write('#INPUT')
		sys.__stdout__.flush()
		data = self.stream.readline()
		if os.linesep != '\n':
			data = data.replace(os.linesep, '\n')
		return data

# First, read the program from stdin.
# TODO: will newlines get changed?
program_length = int(sys.stdin.readline())
program = sys.stdin.read(program_length)

# Change all the streams
sys.stdout = FakeOutput(sys.stdout)
#sys.stderr = sys.stdout # TODO: separate these
sys.stdin  = FakeInput(sys.stdin)

# Now, execute it.
exec program in {}
#sys.stderr.write('I am alive.\n')
#print "Testing"
#print "Testing Again"
##import time
##time.sleep(3)
#print "You typed: " + raw_input()
#sys.stderr.write('I am dying.\n')


# Now, let parent know we're done.
sys.__stdout__.write('##DONE')
sys.__stdout__.flush()
sys.__stdin__.readline()
